// page_two/shebdj/shebdj.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '设备登记',
    page: 1,
    sbList: [],
    gongwu: false,
       // 搜素
       keywordsone:"",
       start: '开始时间',
       endtime: '结束时间',
       begintime: '',
       endtimes: '',
  },


    // 搜索

  
    keywordsone(e) {
      console.log(e)
      var key = e.detail.value
      this.setData({
        keywordsone: key
      })
  
  
      this.getsbList()
    
    },
    bindkaishi(e) {
      this.setData({
        start: e.detail.value,
        begintime: e.detail.value,
      })
      this.getsbList()
  
    },
    bindjieshu(e) {
      if (e.detail.value < this.data.begintime) {
        wx.showToast({
          title: '结束时间不能小于开始时间',
          icon: 'none'
        })
      } else {
        this.setData({
          endtime: e.detail.value,
          endtimes: e.detail.value,
        })
        this.getsbList()
      }
    },
    chonzghia(){
      this.setData({
        keywordsone: '',
        endtimes: '',
        start: '开始时间',
      endtime: '结束时间',
        begintime:'',
      })
      this.getsbList()
  
    },
  
  
  
  // 查看更多
  lookmore() {
    shuzong = this.data.sbList
    this.setData({
      page: (this.data.page) + 1
    })
    this.getsbList()
  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Besall/PlaydeviceDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
          that.getsbList();
      }
    })
  },
  kindToggle(e) {
    const id = e.currentTarget.dataset.id
    let sbList = this.data.sbList
    for (let i = 0, len = sbList.length; i < len; ++i) {
      if (sbList[i].id == id) {
        sbList[i].open = !sbList[i].open
      } else {
        sbList[i].open = false
      }
    }
    this.setData({
      sbList
    })
  },
  getsbList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Besall/PlaydeviceList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      keywords:this.data.keywordsone,
      start_time:this.data.begintime,
      end_time:this.data.endtimes
    }, "POST").then(res => {
      wx.hideLoading()
      shu = res.data.data
      for (let i = 0; i < shu.length; i++) {
        shu[i].open = false;
      }
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          this.setData({
            gongwu: true
          })
        }
      }
      this.setData({
        sbList: shuzong.concat(shu)
      })
      // console.log(res.data.data)
    })
  },
  // 设备报备
  bbjia() {
    wx.navigateTo({
      url: '/page_two/yladdsheb/yladdsheb'
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getsbList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})